package com.xd.study.activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xd.study.R;

/**
 * Created by DGF on 2015/4/28.
 */
public class FragmentPassValueActivity extends Activity implements com.xd.study.fragment.PassValueFragment.IFragmentPassValueToActivity {

    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pass_value);

        editText = (EditText)this.findViewById(R.id.edit_text_pass_value);
        Button button = (Button)this.findViewById(R.id.button_pass_value);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value = editText.getText().toString();

                //activity向fragment传递数据
                com.xd.study.fragment.PassValueFragment fragment = new com.xd.study.fragment.PassValueFragment();
                Bundle bundle = new Bundle();
                bundle.putString("name", value);
                fragment.setArguments(bundle);

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.layout_fragment_pass_value, fragment, "fragment_pass_value");
                fragmentTransaction.commit();

                Toast.makeText(FragmentPassValueActivity.this, "向Fragment发送数据："+value, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 用于接受Fragment的传值
     * @param value
     */
    @Override
    public void thank(String value) {
        Toast.makeText(this, "接收到Fragment的感谢："+value, Toast.LENGTH_SHORT).show();
    }
}
